package ru.ognick.coachsystem.editor;

import ru.ognick.coachsystem.R;
import ru.ognick.coachsystem.common.Exercise;
import ru.ognick.coachsystem.common.Time;
import ru.ognick.coachsystem.common.base.BaseExerciseView;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnFocusChangeListener;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.ToggleButton;

public class EditExerciseView  extends BaseExerciseView {
    public final String TAG = getClass().getSimpleName();
    
	
	private LinearLayout titleBox;
	private LinearLayout content;
	private Button mediaButton;
	
	public EditExerciseView(final Context context, Exercise exercise) {
		super(context, exercise);
	
		LayoutInflater inflator = ((Activity) context).getLayoutInflater();
		view = inflator.inflate(R.layout.editor_exercise, null, true);
		
		content = (LinearLayout) view.findViewById(R.id.ContentExercise);
		hide();
		
		titleBox = (LinearLayout) view.findViewById(R.id.titleBox);
		
		ToggleButton toggle = (ToggleButton) view.findViewById(R.id.toggleButton);
		toggle.setChecked(exercise.isActivity());
		setColorTitleBox();
		toggle.setOnCheckedChangeListener(new OnCheckedChangeListener() {
			@Override
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
				EditExerciseView.this.exercise.setActivity(isChecked);
				setColorTitleBox();
			}
		});
		
		bundleEditToLabel(R.id.nameText, 
				          R.id.nameEdit, 
				          Exercise.Field.name);
		
		bundleEditToLabel(R.id.descriptionText, 
				          R.id.descriptionEdit, 
				          Exercise.Field.description);
		
		bundleTimeToLabel(R.id.durationText,
						  R.id.durationMM,
						  R.id.durationSS,
						  Exercise.Field.duration);
		
		bundleChBoxToFlag(R.id.checkBeep, 
						  Exercise.Field.isBeep);
		
		bundleTime		 (R.id.beepMM,
				   		  R.id.beepSS,
				          Exercise.Field.beepPeriod);
		
		
		mediaButton = (Button) view.findViewById(R.id.mediaButton);
		mediaButton.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
				intent.addCategory(Intent.CATEGORY_OPENABLE);
				((EditorActivity) context).exerciseWaitMedia = EditExerciseView.this;
				intent.setType("audio/*");
				((Activity) context).startActivityForResult(Intent.createChooser(intent, "Open media"), EditorActivity.IDD_MEDIA); 
			}
		});
	
	}
	
	public LinearLayout getTitleBox() {
		return titleBox;
	}
	
	public Button getMediaButton() {
		return mediaButton;
	}
	
	public void hide() {
		content.setVisibility(View.GONE);
	}
	
	public void holdData() {
		for (int i=0; i < content.getChildCount(); i++)
			content.getChildAt(i).requestFocus();
	}
	
	public void show() {
		content.setVisibility(View.VISIBLE);
	}
	
	public boolean autoVisibility() {
		if (content.getVisibility() == View.VISIBLE) {
			hide();
			return false;
		}
		else {
			show();
			return true;
		}
	}
	
///////////////////////////////////////////////////////////////////////////////////
/////////////////////////BUNDLE VIEW AND DATA//////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////
	
	private void bundleEditToLabel(int static_id, int dinamic_id, 
								   final Exercise.Field field){
		String defValue = exercise.getValue(field);
		final TextView staticText = (TextView) view.findViewById(static_id);
		staticText.setText(defValue);
		
		final EditText dinamicEdit = (EditText) view.findViewById(dinamic_id);
		dinamicEdit.setText(defValue);
		dinamicEdit.setTag(defValue);
		dinamicEdit.setOnFocusChangeListener(new OnFocusChangeListener() {
			@Override
			public void onFocusChange(View v, boolean hasFocus) {
				String value = (String) v.getTag(); 
				value = ((EditText) v).getText().toString();
				exercise.setValue(field, value);
				staticText.setText(value);
			}
		});
	}
	
	private int getTimeOfEdit(EditText edit, int maxValue) {
		try {
			int t = Integer.valueOf(edit.getText().toString());
			if (t > maxValue) 
				return maxValue;
			return t;
		}
		catch (NumberFormatException e) {
			return 0;
		}
	}
	
	private void bundleTimeToLabel(int static_id, int min_id, int sec_id, 
								   final Exercise.Field field) {
		Time defTime = exercise.getValue(field);
		final TextView staticText = (TextView) view.findViewById(static_id);
		staticText.setText(defTime.toString());
		bundleTimeToLabel(min_id, sec_id, field, staticText);
	}
	
	private void bundleTime(int min_id, int sec_id, final Exercise.Field field) {
		bundleTimeToLabel(min_id, sec_id, field, null);
	}
	
	private void bundleTimeToLabel(int min_id, int sec_id,
								   final Exercise.Field field,	
			                       final TextView staticText) {
		Time defTime = exercise.getValue(field);
		final EditText mmEdit = (EditText) view.findViewById(min_id);
		mmEdit.setText(String.valueOf(defTime.getMin()));
		mmEdit.setTag(defTime);
		
		final EditText ssEdit = (EditText) view.findViewById(sec_id);
		ssEdit.setText(String.valueOf(defTime.getSec()));
		ssEdit.setTag(defTime);
		
		OnFocusChangeListener l = new OnFocusChangeListener() {
			@Override
			public void onFocusChange(View v, boolean hasFocus) {
				int min = getTimeOfEdit(mmEdit, 60);
				mmEdit.setText(String.valueOf(min));
				int sec = getTimeOfEdit(ssEdit, 60);
				ssEdit.setText(String.valueOf(sec));
				Time time = (Time) v.getTag();
				exercise.setValue(field, time);
				time.setMin(min);
				time.setSec(sec);
				if (staticText != null)
					staticText.setText(time.toString());
			}
		};
		
		mmEdit.setOnFocusChangeListener(l);
		ssEdit.setOnFocusChangeListener(l);
		
	}
		
	private void bundleChBoxToFlag(int id, final Exercise.Field field){
		CheckBox checkBox = (CheckBox) view.findViewById(id);
		boolean defValue = exercise.getValue(field);
		checkBox.setChecked(defValue);
		checkBox.setTag(defValue);
		checkBox.setOnCheckedChangeListener(new OnCheckedChangeListener() {
			@Override
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
				boolean value = (Boolean) buttonView.getTag();
				value = isChecked;
				exercise.setValue(field, value);
			}
		});
	}
	
	private void setColorTitleBox() {
		int colorText = (exercise.isActivity())?Color.GREEN:Color.GRAY;
		for (int i=0; i < titleBox.getChildCount(); i++)
			((TextView) titleBox.getChildAt(i)).setTextColor(colorText);
	}
}
